﻿using System;
using System.Collections.Generic;
using System.Text;

using DotSpatial.Drawing;
using System.Drawing ;

using System.Drawing.Design;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

using DotSpatial.Geometries;
using DotSpatial.Data;
using DotSpatial.Map;

namespace HuanGis
{

    public class PolygonSymbolEditor : UITypeEditor
    {

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {

            IWindowsFormsEditorService DlgProvider;
            DlgProvider = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (DlgProvider != null)
            {
                DotSpatial.Forms.DetailedPolygonSymbolDialog MyPlgSymDlg = new DotSpatial.Forms.DetailedPolygonSymbolDialog((PolygonSymbolizer)value);
                if (DlgProvider.ShowDialog(MyPlgSymDlg) == DialogResult.OK)
                {
                    return MyPlgSymDlg.Symbolizer;
                }
                else
                    return value;

            }
            else
                return value;

            //return base.EditValue(context, provider, value);
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
            //return base.GetEditStyle(context);
        }

    }



    public class FEPolygonLayer : FEditableLayer
    {
        public FEPolygonLayer(string id)
            : base(id)
        {
            this.m_Type = FeatureTypes.Polygon;
        }



        #region 属性

        private object _PolySymbol = new PolygonSymbolizer();

        [Editor(typeof(PolygonSymbolEditor), typeof(UITypeEditor))]
        [Category("基体信息"), DisplayName("面基本信息设置")]
        public object PolygonSymbol
        {
            get { return _PolySymbol; }
            set 
            {
                this._PolySymbol = value;
                this.OnSetSymbol();
            }

        }

        protected void OnSetSymbol()
        {
            if (this.BRunning)
                (this.ILayer as IFeatureLayer).Symbolizer = (PolygonSymbolizer)this._PolySymbol;        
        }

        //---------------------------------------------------------

        private object _SelectSymbol = new PolygonSymbolizer();       

        [Editor(typeof(PolygonSymbolEditor), typeof(UITypeEditor))]
        [Category("基体信息"), DisplayName("所选图形信息设置")]
        public object SelectionSymbol
        {
            get { return _SelectSymbol; }
            set
            {
                this._SelectSymbol = value;
                this.OnSetSelectSymbol();
            }
        }

        protected void OnSetSelectSymbol()
        {
            if (this.BRunning)
                (this.ILayer as IFeatureLayer).Symbolizer = (PolygonSymbolizer)this._SelectSymbol;
        }




        #endregion


        public override void AddShape(FShape shp)
        {
            base.AddShape(shp);

            if (shp.normalPts == null || shp.normalPts.Count < 3) return;

            Shape polyshp = new Shape(FeatureTypes.Polygon);
            polyshp.AddPart(shp.normalPts.ToArray(), CoordinateTypes.Regular);

            Feature f = new Feature(polyshp);

            if (f != null)
                (this.ILayer.DataSet as IFeatureSet).Features.Add(f);           
        }

        public override void AddShapeRane(List<FShape> lstShp)
        {
            base.AddShapeRane(lstShp);

            for (int i = 0; i < lstShp.Count; i++)
            {
                this.AddShape(lstShp[i]);
            }
           
        }
    }
}
